﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SgLib;

public class PlatformGenerate : MonoBehaviour
{
    public Transform[] Blocks;
   
    private Camera cam;
    private int sizeObstacles;

    public GameObject[] Obstacles;
    public GameObject coin;
    public float maxPosX = -2f;
    public float camPosX;
    public float spriteWidth;

    private int randSize;
    private float randX;
    private float randY;

    private void Awake()
    {
        sizeObstacles = Obstacles.Length;
      
        cam = Camera.main;     
    }

    // Use this for initialization
    void Start()
    {
        spriteWidth = Mathf.Abs(Blocks[1].position.x - Blocks[2].position.x);

        foreach (Transform t in Blocks)
        {
            
            if (t.position.x > maxPosX)
            {
                maxPosX = t.position.x;
            }
            
        }
        randSize = Blocks.Length;
    }

    // Update is called once per frame
    void Update()
    {       
        camPosX = cam.transform.position.x;

        if (cam.transform.position.x > maxPosX)/*&&(GameManager.Instance.GameState == GameState.Playing))*/
        {
           
            randSize = Blocks.Length - Random.Range(0, Blocks.Length - 1);
            randY = Random.Range(-0.7f, 0.7f);
            randX = Random.Range(1.7f, 2.7f);
            ObstacleGenerate();
            BlockGenerated();           
        }
    }

    void ObstacleGenerate()
    {
        int minBlock = FindMinBlock();
        //float randY = Random.Range(-1f, 1f);
        //float randX = Random.Range(0, 2f);
        int maxBlock = FindMaxBlock();
        spriteWidth = (float)Blocks[maxBlock].GetComponent<SpriteRenderer>().bounds.size.x;
        float spriteWidth2 = (float)Blocks[minBlock].GetComponent<SpriteRenderer>().bounds.size.x;

        float distance = spriteWidth / 2 + spriteWidth2 / 2;

        CoinGenerate(distance);
        if (spriteWidth2 > 14)
        {
            int randomNumber = Random.Range(2, 4);
            float distanceEnemy = spriteWidth2 / (randomNumber + 1);
            float increaseDis = Random.Range(distanceEnemy - 0.5f, distanceEnemy + 0.5f);
            for (int i = 0; i < randomNumber; i++)
            {
                int numObstacle = Random.Range(0, sizeObstacles);
                Vector3 obstaclePos = new Vector3(maxPosX + spriteWidth / 2 + randX + increaseDis, 0 + 1.2f, transform.position.z);
                Instantiate(Obstacles[numObstacle], obstaclePos, Quaternion.identity);
                increaseDis += Random.Range(distanceEnemy - 0.5f, distanceEnemy + 0.5f);
            }

        }
        else if (spriteWidth2 > 10)
        {
            int randomNumber = Random.Range(1, 4);
            float distanceEnemy = spriteWidth2 / (randomNumber + 1);
            float increaseDis = Random.Range(distanceEnemy - 0.3f, distanceEnemy + 0.3f);
            for (int i = 0; i < randomNumber; i++)
            {
                int numObstacle = Random.Range(0, sizeObstacles);
                Vector3 obstaclePos = new Vector3(maxPosX + spriteWidth / 2 + randX + increaseDis, 0 + 1.2f, transform.position.z);
                Instantiate(Obstacles[numObstacle], obstaclePos, Quaternion.identity);
                increaseDis += Random.Range(distanceEnemy - 0.3f, distanceEnemy + 0.3f);
            }

        }
        else if (spriteWidth2 > 8.5)
        {
            int randomNumber = Random.Range(1, 3);
            float distanceEnemy = spriteWidth2 / (randomNumber + 1);
            float increaseDis = Random.Range(distanceEnemy - 0.5f, distanceEnemy + 0.5f);
            for (int i = 0; i < randomNumber; i++)
            {
                int numObstacle = Random.Range(0, sizeObstacles);
                Vector3 obstaclePos = new Vector3(maxPosX + spriteWidth / 2 + randX + increaseDis, 0 + 1.2f, transform.position.z);
                Instantiate(Obstacles[numObstacle], obstaclePos, Quaternion.identity);
                increaseDis += Random.Range(distanceEnemy - 0.5f, distanceEnemy + 0.5f);
            }
        }
        else if (spriteWidth2 > 4)
        {
            int numObstacle = Random.Range(0, sizeObstacles);
            Vector3 obstaclePos = new Vector3(maxPosX + distance + randX, 0 + 1.2f, transform.position.z);
            Instantiate(Obstacles[numObstacle], obstaclePos, Quaternion.identity);
        }
    }

    int FindMinBlock()
    {
        float min = 9000000;
        int id = 1;
        for (int i = 0; i < randSize; i++)
        {
            if (Blocks[i].position.x < min)
            {
                min = Blocks[i].position.x;
                id = i;
            }
        }
        return id;
    }

    int FindMaxBlock()
    {
        float max = -9000000;
        int id = 1;
        for (int i = 0; i < Blocks.Length; i++)
        {
            if (Blocks[i].position.x > max)
            {
                max = Blocks[i].position.x;
                id = i;
            }
        }
        return id;
    }


    void CoinGenerate(float distance)
    {
        float rand = Random.value;
        if (rand < GameManager.Instance.coinFrequency)
        {
           
            Vector3 obstaclePos = new Vector3(maxPosX + distance + randX, randY + 1f, transform.position.z);
            Instantiate(coin, obstaclePos, Quaternion.identity);
        }
    }

    void BlockGenerated()
    {
        
        int minBlock = FindMinBlock();
       
    
        int maxBlock = FindMaxBlock();
        spriteWidth = (float)Blocks[maxBlock].GetComponent<SpriteRenderer>().bounds.size.x;
        float spriteWidth2 = (float)Blocks[minBlock].GetComponent<SpriteRenderer>().bounds.size.x;

        Blocks[minBlock].transform.position = new Vector3(maxPosX + spriteWidth / 2 + spriteWidth2 / 2 + randX, this.transform.position.y + randY, this.transform.position.z);
        maxPosX = Blocks[minBlock].transform.position.x;
    }
}
